<!DOCTYPE html>
<!-- saved from url=(0061)file:///C:/Users/ASUS/Desktop/suanfademo/.vscode/hongbao.html -->
<html lang="en"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .main{
            width: 300px;
            height: 100%;
            margin: 0 auto;
            background-color:wheat;
            border: 1px solid wheat;
        }
        .head{
            background-color: wheat;
            text-align: center;
            color: crimson;
            width: 100%;
            height: 200px;
            font-family: 'Courier New', Courier, monospace;
            font-weight: 300;
            font-size: large;
        }
        /* css垂直居中 */
        .head h1{
            margin:0 auto;
            position: relative;
            top: 50%;
            margin-top: -50px;
        }
    </style>
</head>
<body>



      <div class="main">
        <div class="head">
            <h1> 一年一度的员工抽奖活动来了，欢迎大家</h1>
       </div>
       <input type="text" name="number" class="num" placeholder="请输入员工人数">
       <input type="text" name="totalmoney" class="money" placeholder="请输入投放金额">
       <button onclick="fenpai()" style="background-color: wheat;border-radius: 10%;">分奖金</button>

      <table class="tol" border="1" style="background-color: bisque;width: 100%;color: red;">
         <tbody><tr>
             <th>员工抽奖金额分配</th>
        </tr>
        <tr class="employee">
       </tr>
      </tbody></table>
    </div>

    <script>
// dom节点的操作和遍历
function fenpai() {
    document.querySelector('.employee').innerHTML = ''
    var num = document.querySelector('.num').value
    var money = document.querySelector('.money').value
    var mon=assign(money,num)
    for (let index = 0; index< mon.length; index++) {
        const element = mon[index];
        var td1 = document.createElement("div")
        td1.style="margin:10px"

        td1.innerHTML = '员工编号'+index+'  '+'奖金金额: '+element

        document.querySelector(".employee").appendChild(td1)

    }
    document.querySelector('.num').value = ''
    document.querySelector('.money').value = ''

}
// 随机分配算法
 function assign(totalAmount, totalPeople){
  var remainAmount = +totalAmount;
  var remainPeople = +totalPeople;
  var arr = [];
  while(remainPeople > 0){
    let num = scramble(remainAmount, remainPeople);
    remainAmount = remainAmount - num;
    remainPeople--;
    arr.push(num);
  }
  return arr;
}
function scramble(remainAmount, remainPeople){
  if(remainPeople === 1){
    return +remainAmount.toFixed(2);
  }
  let max = ((remainAmount / remainPeople) * 2 - 0.01).toFixed(2);
  let min = 0.01;
  let range = max - min;
  let rand = Math.random();
  let num = min + Math.round(rand * range);
  return num;
}
    </script>

</body></html>
